//
//  JCEmotionToolBar.swift
//  SinaWeibo_18
//
//  Created by 魏欣宇 on 15/10/30.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit
//表情类型
enum JCEmotionToolBarButtonType: Int{
    case Recent = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}

protocol JCEmotionToolBarDelegate: NSObjectProtocol {
    func emotionToolBar(toolBar: JCEmotionToolBar, buttonClickType: JCEmotionToolBarButtonType)
}

class JCEmotionToolBar: UIView {
    
    //代理
    weak var delegate: JCEmotionToolBarDelegate?

    var currentSelectedButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//    backgroundColor = randomColor()
        
        //添加4个按钮
        addChildButton("最近", imageName: "compose_emotion_table_left", type: .Recent)
        addChildButton("默认", imageName: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", imageName: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", imageName: "compose_emotion_table_right", type: .Lxh)
    }
    
    private func addChildButton(title: String, imageName: String, type: JCEmotionToolBarButtonType){
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //文字
        button.setTitle(title, forState: UIControlState.Normal)
        //字体
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        //字体颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        //不同状态下图片
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        //设置当前按钮的tag值   区分点击的是哪个按钮
        button.tag = type.rawValue
        
        addSubview(button)
        
    }
    
    @objc private func childButtonClick(button: UIButton){
        //点击相同的按钮  直接返回  
        if currentSelectedButton == button {
            return
        }
        //选择按钮
        currentSelectedButton?.selected = false
        
        button.selected = true
        
        currentSelectedButton = button
        //通知代理
        if let del = delegate {
            del.emotionToolBar(self, buttonClickType: JCEmotionToolBarButtonType(rawValue: button.tag)!)
        }
        
    }
    
    // MARK: - 根据表情类型选择某一个表情按钮
    func selectedButtonWithType(type: JCEmotionToolBarButtonType){
        
        let button = self.viewWithTag(type.rawValue) as! UIButton
        //选中当前按钮 取消上一个选择按钮
        if currentSelectedButton != button {
            button.selected = true
            currentSelectedButton?.selected = false
            //记录
            currentSelectedButton = button
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let childW = width / CGFloat(subviews.count)
        
        for(index,value) in subviews.enumerate() {
            
            value.width = childW
            value.height = height
            value.x = CGFloat(index) * childW
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
}
